import { defineEventHandler, readBody, createError } from "h3";
import auth from "../../../middleware/secure";
const { requireAuth } = auth;
import { LemonSqueezyService } from "../../../services/lemonSqueezyService";

export default defineEventHandler(async (event) => {
  const user = await requireAuth(event);
  const { variantId } = await readBody(event);

  if (!variantId) {
    throw createError({
      statusCode: 400,
      statusMessage: "Variant ID is required",
    });
  }

  const checkoutUrl = await LemonSqueezyService.createCheckoutSession(user.id, variantId);

  return { checkoutUrl };
});